package parser

import (
	"gitee.com/volvoxc60/go-crawler/engine"
	"regexp"
)

const CityListRe = `<a target="_blank" href="(http://www.zhenai.com/zhenghun/[a-z0-9]+)"[^>]*>([^<]+)</a>`

const CityListRe2 = `<a href="(http://www.zhenai.com/zhenghun/[a-z0-9]+)"[^>]*>([^<]+)</a>`

func ParseCityList(contents []byte) engine.ParseResult {

	//<a target="_blank" href="http://www.zhenai.com/zhenghun/shijiazhuang" data-v-f53df81a>石家庄</a>
	res := regexp.MustCompile(CityListRe2)
	//res:=regexp.MustCompile(`<a data-v-1573aa7c="" href="(http://www.zhenai.com/zhenghun/[a-z0-9]+)"[^>]*>([^<]+)</a>`)
	matches := res.FindAllSubmatch(contents, -1)
	result := engine.ParseResult{}
	limit := 3
	for _, m := range matches {
		result.Items = append(result.Items, "City "+string(m[2]))
		result.Requests = append(result.Requests, engine.Request{
			Url:       string(m[1]),
			ParseFunc: ParseCity,
		})
		limit--
		if limit == 0 {
			break
		}
		//fmt.Printf("city is %s\n url is %s\n",string(m[2]),string(m[1]))
		//for _,subMatch:=range m{
		/**
		这里整个match会打一行，然后分别针对某个子匹配打印
		<a target="_blank" href="http://www.zhenai.com/zhenghun/beijing" data-v-f53df81a>北京</a>

		http://www.zhenai.com/zhenghun/beijing
		北京
		*/
		//fmt.Printf("%s\n",subMatch)
		//}

	}
	return result
}
